// #ifdef H5
let Types = {
	isFunction(obj: any) {
		let type = Object.prototype.toString.call(obj)
		return type == '[object Function]'
	},
	isObject(obj: any) {
		let type = Object.prototype.toString.call(obj)
		return type == '[object Object]'
	},
	isString(obj: any) {
		let type = Object.prototype.toString.call(obj)
		return type == '[object String]'
	}
}

function fileDownLoad(data: any) {
	let linkElement = document.createElement('a')
	linkElement.setAttribute('href', data.blob)
	linkElement.setAttribute('downLoad', data.name)
	linkElement.click()
}

// 保存图片到系统相册
uni.saveImageToPhotosAlbum = uni.saveVideoToPhotosAlbum = function (options: any) {
	let emptyFun = () => ({})
	let config: any = { filePath: null, success: emptyFun, fail: emptyFun, complete: emptyFun }
	if (options && Types.isObject(options)) {
		config = { ...config, ...options }
	}
	if (options && Types.isString(options)) {
		config = { ...config, filePath: options }
	}
	let filePath = config.filePath
	let success = config.success || emptyFun
	let fail = config.fail || emptyFun
	let complete = config.complete || emptyFun
	if (!filePath) {
		fail && Types.isFunction(fail) && fail({ msg: 'no File' })
		complete && Types.isFunction(complete) && complete()
		return
	}
	let names = filePath.split('/')
	let name = names[names.length - 1]
	uni.downloadFile({
		url: filePath,
		success(res) {
			let tempFilePath = res.tempFilePath
			fileDownLoad({ name, blob: tempFilePath })
			success && Types.isFunction(success) && success({ filePath })
		},
		fail(err) {
			fail && Types.isFunction(fail) && fail({ msg: err })
		},
		complete() {
			complete && Types.isFunction(complete) && complete()
		}
	})
}
// #endif
